package top.minuy.structure.queue.deque;

import top.minuy.structure.linear.array.Array;
/**
 * @author Minuy
 * @time 22:05
 * @date 2021/8/22
 */
public class ArrayDeque<E> implements Deque<E> {

    private Array<E> array;

    public ArrayDeque(int capacity){
        array = new Array<E>(capacity);
    }

    public ArrayDeque(){
        this(10);
    }

    /**
     * 获取队列大小
     *
     * @return 队列大小
     */
    @Override
    public int getSize() {
        return this.array.getSize();
    }

    /**
     * 判断队列是否为空
     *
     * @return 队列是否为空
     */
    @Override
    public boolean isEmpty() {
        return this.array.isEmpty();
    }

    /**
     * 从队列前面添加元素
     *
     * @param e 要添加的元素
     */
    @Override
    public void addFront(E e) {
        this.array.addFirst(e);
    }

    /**
     * 从队列后面添加元素
     *
     * @param e 要添加的元素
     */
    @Override
    public void addLast(E e) {
        this.array.addLast(e);
    }

    /**
     * 从队列前面移除队列元素
     *
     * @return 被移除的队列元素
     */
    @Override
    public E removeFront() {
        if(this.isEmpty()){
            throw new IllegalArgumentException("RemoveFront Failed. Deque is empty.");
        }
        return this.array.removeFirst();
    }

    /**
     * 从队列后面移除队列元素
     *
     * @return 被移除的队列元素
     */
    @Override
    public E removeLast() {
        if(this.isEmpty()){
            throw new IllegalArgumentException("RemoveLast Failed. Deque is empty.");
        }
        return this.array.removeLast();
    }

    /**
     * 获取队列容量
     * @return 队列容量大小
     */
    public int getCapacity(){
        return this.array.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array Deque : size = %d , capacity = %d\n", this.array.getSize(), this.getCapacity()));
        res.append("front [");
        for (int i = 0; i < this.getSize(); i++) {
            res.append(this.array.get(i));
            if (i != this.getSize() - 1) {
                res.append(", ");
            }
        }
        res.append("] tail");
        return res.toString();
    }
}
